package cn.iocoder.yudao.module.chat.service.gpt;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.websocket.core.WebSocketUser;
import cn.iocoder.yudao.module.chat.controller.app.gpt.vo.ChatMessageVO;
import cn.iocoder.yudao.module.gpt.api.ChatApi;
import cn.iocoder.yudao.module.gpt.api.dto.GPTSessionResponseDTO;
import cn.iocoder.yudao.module.gpt.api.dto.GPTUserMessageDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

@Service
public class ChatServiceImpl implements ChatService{
    @Resource
    private ChatHistoryService chatHistoryService;

    @Resource
    private ChatSessionService chatSessionService;
    @Resource
    private ChatApi chatApi;

    @Override
    public void executeChat(ChatMessageVO message) {
        GPTUserMessageDTO gptUserMessageDTO = new GPTUserMessageDTO();
        gptUserMessageDTO.setContent(message.getContent());
        //gptUserMessageDTO.setSessionId();

    }

    @Override
    public String registerChatSession() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser,"LoginUser will never be null,please check Authorization System");
        if(loginUser instanceof WebSocketUser websocketUser){
            CommonResult<GPTSessionResponseDTO> response = chatApi.requestSessionId(websocketUser.getId(),websocketUser.getSessionId());
            if(response.isSuccess()){
                chatSessionService.createChatSession(loginUser.getId(),websocketUser.getSessionId());
                return response.getData().getSessionId();
            }
        }
        return null;
    }
}
